<template>
	<view class="page-login">	
		<view class="logo">
			<!-- <van-image round width="160rpx" height="160rpx" src="http://picture.youjiahu.com/mp_app/client/logo.png" /> -->
      <view class="name">like sport</view>
      <view class="slogan">热爱运动 立刻运动</view>
		</view>
		<view class="btn-group">
			<van-button v-if="is_register" type="primary" round block size="big" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" @click="getUserProfile" :loading="loading">微信登录/注册</van-button>
			<van-button v-else type="primary" round block size="big"  @click="loginByWechat" :loading="loading">微信登录/注册</van-button>
			<van-button block round size="big" @tap="$utils.gotoPage('./phone')">手机号登录/注册</van-button>
		</view>
		<view class="agree-box">
			<van-checkbox checked-color="#3678FD" icon-size="34rpx" :value="is_agree" @change="changeCheck">
				已阅读并同意<text class="c-blue" @tap.stop.prevent="$utils.gotoPage('./agreement')">《用户服务协议》</text>
			</van-checkbox>			
		</view>

		<view class="service">登录遇到问题？请<text class="c-blue" @tap="callService">咨询客服</text></view>
	</view>
</template>

<script>
import login from '@/static/mixins/login';
export default {
	mixins: [login],
  data(){
    return{
      is_agree: true,
			is_register: false,
			loading: false,
			user_info: {},
			login_params: {},
    }
  },
	onShow(){
		this.user_info = uni.getStorageSync('userInfo');
		this.is_register = this.user_info ? false : true;
	},
	methods:{
		changeCheck(e){
			this.is_agree = !this.is_agree;
		},
		getPhoneNumber(e){
			this.login_params.phoneCode = e.detail.code;
			if(this.login_params.userInfo){
				this.loginByWechat()
			}
		},
		getUserProfile(){
			uni.getUserProfile({
				desc: '用于完善会员资料',
				success: infoRes => {
					this.login_params.userInfo = infoRes.userInfo;
					if(this.login_params.phoneCode){
						this.loginByWechat();
					}
				}
			})
		},
		// 微信登录
		loginByWechat(){
			uni.login({
				provider: 'weixin',
				success: loginRes => {
					this.login_params.loginCode = loginRes.code;
					this.$http('post', '/auth/wxlogin', this.login_params, (data,msg) => {
						this.login_params = {};
						uni.setStorageSync('access_token', data.access_token);
						this.$store.commit('setAccessToken',data.access_token);
						this.getUserInfo();
						setTimeout(() => {
							this.backFromLoginPage();
						},1000);
					}, msg => {
						this.login_params = {};
					});	
				}
			})
		},
	}
}
</script>

<style>

</style>